import 'package:flutter/material.dart';
import 'package:flutter_animation/widget/particle/particle_model.dart';

class ParticlePainter extends CustomPainter {
  List<ParticleModel> particles;
  Duration time;

  ParticlePainter(this.particles, this.time);

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()..color = Colors.white.withAlpha(50);
    // final paint = Paint()..color = Colors.red[700].withAlpha(90);
    particles.forEach((particle) {
      var progress = particle.animationProgress.progress(time);
      final animation = particle.tween.transform(progress);
      final position =
          Offset(animation["x"] * size.width, animation["y"] * size.height);
      // 圆形
      // canvas.drawCircle(position, size.width * 0.2 * particle.size, paint);
      // 画心
      double heratSize = size.width * 0.38 * particle.size;
      double heratHeight = heratSize * 1.4;
      Path heartPath = new Path()
        ..moveTo(heratSize / 2, heratHeight / 4)
        ..cubicTo((heratSize * 6) / 7, heratHeight / 9, (heratSize * 13) / 13,
            (heratHeight * 2) / 5, heratSize / 2, (heratHeight * 7) / 12)
        ..moveTo(heratSize / 2, heratHeight / 4)
        ..cubicTo(heratSize / 7, heratHeight / 9, heratSize / 21,
            (heratHeight * 2) / 5, heratSize / 2, (heratHeight * 7) / 12);
      canvas
        ..save()
        ..translate(position.dx, position.dy)
        ..drawPath(heartPath, paint)
        ..restore();
    });
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}
